package com.itao.jdk;

import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @Author sjt
 * @CreateTime 2023/10/15 13:08
 */
public class DomTest {

    public static void main(String[] args) throws Exception{
        //read();
        //readByXpath();
        write();
    }

    private static void read() throws Exception {
        var factory = DocumentBuilderFactory.newInstance();
        var builder = factory.newDocumentBuilder();
        //var is = DomTest.class.getClassLoader().getResourceAsStream("read.xml");
        var document = builder.parse("E:\\idea\\java-learn\\xml\\src\\main\\resources\\read.xml");
        // 获取根元素
        var element = document.getDocumentElement();
        System.out.println(element.getTagName());
        // 根据标签名获取元素列表
        var elements = document.getElementsByTagName("student");
        for (int i = 0; i < elements.getLength(); i++){
            var e = (Element)elements.item(i);
            System.out.println(e.getTagName());
        }
    }

    private static void readByXpath() throws Exception {
        var factory = DocumentBuilderFactory.newInstance();
        var builder = factory.newDocumentBuilder();
        var is = DomTest.class.getClassLoader().getResourceAsStream("read.xml");
        var document = builder.parse(is);
        var xPathFactory = XPathFactory.newInstance();
        var xPath = xPathFactory.newXPath();
        // 获取标签名为student并且no属性值为1的元素
        var expression = xPath.compile("//student[@no='1']");
        var evaluate = expression.evaluate(document, XPathConstants.NODE);
        var student = (Element) evaluate;
        System.out.println(student.getTagName());
    }

    private static void write() throws Exception {
        var factory = DocumentBuilderFactory.newInstance();
        var builder = factory.newDocumentBuilder();
        var document = builder.newDocument();
        var school = document.createElement("school");

        var aClass = document.createElement("class");
//        var classNo = document.createAttribute("no");
//        classNo.setValue("1801");
//        aClass.setAttributeNode(classNo);
        aClass.setAttribute("no", "1801");

        var student = document.createElement("student");
        var studentNo = document.createAttribute("no");
        studentNo.setValue("1");
        student.setAttributeNode(studentNo);

        var name = document.createElement("name");
        name.setTextContent("张鹏飞");
        var sex = document.createElement("sex");
        sex.setTextContent("男");
        var addr = document.createElement("addr");
        addr.setTextContent("武汉");

        // 层级关系
        document.appendChild(school);
        school.appendChild(aClass);
        aClass.appendChild(student);
        student.appendChild(name);
        student.appendChild(sex);
        student.appendChild(addr);

        // 写出
        var transformerFactory = TransformerFactory.newInstance();
        var transformer = transformerFactory.newTransformer();
        // 格式化输出
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        // 编码
        transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        var source = new DOMSource(document);
        //var path = DomTest.class.getClassLoader().getResource("").getPath();
        var fos = new FileOutputStream("E:\\idea\\java-learn\\xml\\src\\main\\resources\\dom-write.xml");
        var result = new StreamResult(fos);
        transformer.transform(source, result);
    }
}
